<template>
  <div class="edit-data-view">
    <h1>编辑数据页面</h1>
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="索引" prop="id">
        <el-input v-model="form.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="菜单分类" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getCategory, updateCategory } from "@/api/user"; // 导入获取和更新分类的 API 方法

export default {
  data() {
    return {
      form: {
        id: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入菜单分类', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    goBack() {
      this.$router.push('/'); // 返回到数据列表页面
    },
    fetchCategory() {
      const id = this.$route.params.id; // 从路由参数中获取 ID
      getCategory(id).then((res) => {
        this.form = res.data; // 假设返回的数据结构是 { data: { id, name } }
      }).catch(error => {
        this.$message.error('获取分类信息失败：' + error.message);
      });
    },
    submitForm() {
      updateCategory(this.form).then(() => {
        this.$message.success('修改成功');
        this.goBack(); // 提交成功后返回
      }).catch(error => {
        this.$message.error('修改失败：' + error.message);
      });
    }
  },
  created() {
    this.fetchCategory(); // 页面创建时获取分类信息
  }
};
</script>

<style scoped>
.edit-data-view {
  padding: 20px;
}
</style> 